Ext.application({
    name: 'App',
    extend: 'Extend.app.AjaxSecurityApplication',
    appFolder: 'app',
    requires: [
        'App.InitView',
        'App.main.view.MainViewport'],
    mainViewClass: 'App.main.view.MainViewport',
    mainView: 'App.InitView',
    init: function (application) {
        Ext.log.info('development:true');
        Ext.log.info('libsPath:' + libsPath);
        Ext.log.info('extjsPath:' + extjsPath);
        Ext.log.info('extendPath:' + extendPath);
        Ext.log.info('theme:' + theme);

        window.app = window.App = window.Application = window.application = application;
        window.app.optsQueue = [];
        Ext.Ajax.disableCachingParam = 'dc';

        // this.custominit();
        //this.checkBrowser();
        var me = this;
        Ext.Ajax.request({
            url: 'load_initial_information',
            success: function (resp) {
                var result = resp.result;
                app.localhost = result.data.localhost;
                app.user = result.data.user;
                app.project = result.data.project;
                if (me.getMainView().isMasked()) {
                    me.getMainView().unmask();
                }
                me.getMainView().destroy();
                me.setMainView(me.mainViewClass);
                Ext.log({
                    level: 'info',
                    msg: app.localhost
                })
            }
        });
        window.onbeforeunload = function () {
            if (!Ext.Object.isEmpty(app.editors)) {
                return "存在编辑中的数据还没有保存，是否要离开此页面？";
            }
            return null;
        }


    },
    showLoginWindow: function () {
        Ext.create('App.authorization.user.view.LoginWindow', {}).show();
    },
    checkBrowser: function () {
        if ((Ext.browser.name == 'Chrome' && Ext.browser.version.major < 42)
            || (Ext.browser.name == 'IE' && Ext.browser.version.major < 10)) {
            Extend.Msg
                .warn(
                    '提示',
                    '为了更好的用户体验，建议浏览器版本：Google chrome 42.xxx及以上版本、IE10及以上版本。<br/><br/>'
                    + '<span>Google Chrome：<a style="color:blue;" href="software/ChromeStandalone_49.0.2623.110_Setup.1459233395.exe">ChromeStandalone_49.0.2623.110_Setup.1459233395</a></span><br/>'
                    + 'Internet Explorer 11 32位：<a  style="color:blue;" href="software/Internet Explorer11_ ForWin7_32bit_201610.exe">Internet Explorer 11 for Win7_32bit</a><br/>'
                    + 'Internet Explorer 11 64位：<a  style="color:blue;" href="software/Internet Explorer11_ ForWin7_64bit_201610.exe">Internet Explorer 11 for Win7_64bit</a>');
        }
    }
    ,
    checkAuthorities: function (view) {
        var mainView = this.getMainView();
        var target = view || mainView;
        var list = target.query('component[authorization="true"]');
        if (Ext.isEmpty(list) || list.length == 0) {
            return;
        }
        if (Ext.isEmpty(this.user) || Ext.isEmpty(app.user.resources)) {
            Ext.Array.each(list, function (item) {
                item.hide();
            })
        } else {
            Ext.Array.each(list, function (item) {
                var uiid = item.uiid;
                var authorities = Ext.Array.findBy(app.user.resources,
                    function (resource) {
                        if (!Ext.isEmpty(uiid.match(resource.uiid))) {
                            return item;
                        }
                    })
                if (Ext.isEmpty(authorities)) {
                    item.hide();
                } else {
                    item.show();
                }
            })
        }
    }
    ,
    custominit: function () {
        window.onkeydown = function (e) {
            if (event.keyCode == 8) {
                if (e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA"
                    && e.target.className != "dotitem") {
                    if (app.app.optsQueue.length > 1) {
                        var lastIndex = app.app.optsQueue.length - 2;
                        var item = app.app.optsQueue[lastIndex];
                        Ext.Array.removeAt(app.app.optsQueue, lastIndex, 10);
                        app.app._mainView.down('North').fireEvent('menuclick',
                            item.clazz, item.options)
                    }
                    console.log(app.app._mainView)
                    if (Ext.isFunction(e.stopEvent)) {
                        e.stopEvent();
                    }
                    if (Ext.isFunction(e.preventDefault)) {
                        e.preventDefault();
                    }
                }
            }
        };

        Ext.override(Ext.grid.plugin.RowEditing, {
            onCellClick: function (view, cell, colIdx, record, row, rowIdx, e) {
                if (this.clicksToEdit === false || this.clicksToMoveEditor === false) {
                    return;
                }
                // Make sure that the column has an editor. In the case of
                // CheckboxModel,
                // calling startEdit doesn't make sense when the checkbox is
                // clicked.
                // Also, cancel editing if the element that was clicked was a
                // tree expander.
                var expanderSelector = view.expanderSelector,
                    // Use getColumnManager() in this context because colIdx
                    // includes hidden columns.
                    columnHeader = view.ownerCt.getColumnManager().getHeaderAtIndex(colIdx), editor = columnHeader
                        .getEditor(record);

                if (this.shouldStartEdit(editor)
                    && (!expanderSelector || !e.getTarget(expanderSelector))) {
                    view.ownerGrid.setActionableMode(true, e.position);
                }
            }
        });

// 解决Model创建后日期字段相关8小时问题
// 重写Model日期格式转换问题
// 原数据带T，原来在处理这样的日期时会导致时间+8小时问题
// 将原数据的T替换成“ ”解决。
        Ext.override(Ext.data.field.Date, {
            convert: function (v) {
                if (!Ext.isEmpty(arguments) && Ext.isString(arguments[0])
                    && !Ext.isEmpty(arguments[0])) {
                    arguments[0] = arguments[0].replace(/T/g, ' ')
                }
                return this.callParent(arguments);
            }
        });

        Ext.override(Ext.util.Format, {
            percent: function (value, formatString) {
                var me = this;
                if (value == -987654321) {
                    return '-';
                }
                var xxx = me.number(value * 100, formatString || '0')
                    + me.percentSign;
                return xxx;
            },
            round: function (value, precision) {
                var result = Number(value);
                if (result == -987654321) {
                    return '-';
                }
                if (typeof precision === 'number') {
                    precision = Math.pow(10, precision);
                    result = Math.round(value * precision) / precision;
                } else if (precision === undefined) {
                    result = Math.round(result);
                }
                return result;
            }
        });

    }
})